﻿using Manon.Core.Commons;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Manon.Core.Extensions
{
    public class CompareHelper
    {
        public static IEnumerable<CompareInfo> Compare<T>(T modifyBefore, T modifyAfter) where T : class, new()
        {

            List<CompareInfo> compareInfos = new List<CompareInfo>();
            Type t = modifyBefore.GetType();
            PropertyInfo[] propertyList = t.GetProperties();
            foreach (PropertyInfo item in propertyList)
            {
                string name = item.Name;
                DescriptionAttribute attribute = item.GetCustomAttribute<DescriptionAttribute>();

                object valueBefore = item.GetValue(modifyBefore, null);

                object valueAfter = item.GetValue(modifyAfter, null);

                if (valueBefore.ToString() != valueAfter.ToString())
                {
                    compareInfos.Add(new CompareInfo()
                    {
                        Code = name,
                        Desc = attribute?.Description,
                        ModifyAfter = valueAfter.ToString(),
                        ModifyBefore = valueBefore.ToString()
                    });
                }
            }

            return compareInfos;
        }
    }
}
